package com.dhecp.project.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.system.domain.SysIndexModule;
import com.dhecp.project.system.domain.SysUserHomeDataInfo;
import com.dhecp.project.system.domain.SysUserHomeMessage;
import com.dhecp.project.system.mapper.SysIndexModuleMapper;
import com.dhecp.project.system.service.SysIndexModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 用户首页
 * @author DH-Xxj
 * @version 2019年9月19日
 */

@Service
public class SysIndexModuleServiceImpl implements SysIndexModuleService {

	@Autowired
	private SysIndexModuleMapper indexModuleMapper;

	/**
	 * 查询首页常用栏目
	 * @param params
	 * @return List - SysIndexModule
	 * @author DH-Xxj
	 */
	@Override
	public List<SysIndexModule> queryTableList(Map<String, Object> params){
		return indexModuleMapper.findTableList(params);
	}

	/**
	 * 保存首页常用栏目
	 * @param moduleTable 必要参数（模块名）
	 * @return AjaxResult
	 * @author kimiloo
	 */
	@Override
	public AjaxResult insertIndexModule(Map<String, Object> params){
		List<SysIndexModule> indexModuleList = JSON.parseArray(params.get("infoItems").toString(), SysIndexModule.class);
//		if (indexModuleList == null || indexModuleList.size() == 0) {
//			return AjaxResult.error(MessageUtils.message("module.error.jsontoentyerror"));
//		}
		indexModuleMapper.deleteIndexModuleByUserid(ShiroUtils.getUserId());
		for (SysIndexModule indexModuleInfo : indexModuleList) {
			indexModuleInfo.setaUser(ShiroUtils.getUserId());
			indexModuleMapper.insertIndexModule(indexModuleInfo);
		}
		return AjaxResult.success("操作成功");
	}

	/**
	 * 查询首页消息提醒
	 * @param params
	 * @return List - SysUserHomeMessage
	 * @author DH-Xxj
	 */
	@Override
	public List<SysUserHomeMessage> queryUserHomeMessageList(Map<String, Object> params){
		String userId = ShiroUtils.getMandatorId();
		return indexModuleMapper.findUserHomeMessage(userId);
	}

	/**
	 * 查询首页预警信息
	 * @param params
	 * @return List - SysUserHomeDataInfo
	 * @author DH-Xxj
	 */
	@Override
	public List<SysUserHomeDataInfo> queryUserHomeDataInfoList(Map<String, Object> params){
		String userId = ShiroUtils.getMandatorId();
		return indexModuleMapper.findUserHomeDataInfo(userId);
	}

}
